<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}

function GetRightList($select)
{
	global $PortalPref;

	$query=mysql_query("SELECT * FROM ".$PortalPref."right ORDER BY right_value ASC");

	$rightList='';

	while($result=mysql_fetch_assoc($query))
	{
		if ($select==$result['right_value'])
			$selected='selected';
		else
			$selected='';
		$rightList.='<option value="'.$result['right_value'].'" '.$selected.'>'.pEcho($result['right_title']).'</option>';
	}

	return $rightList;
}

function GetOptionTypeVaLue($value_setted, $value_str, $type, $output)
{
	$typeValue=array();
	$FunctionResult='';


	$value_array = explode("|", $value_str);
	$typeValue['value']=explode(",", $value_array[0]);
	$typeValue['variant']=explode(",", $value_array[1]);
	$key=array_search($value_setted, $typeValue['value']);

	if($type=='chkbx')
	{
		if ($output=='table')
			$FunctionResult=pEcho($typeValue['variant'][$key]);

		if($output=='form')
		{
			if ($typeValue['value'][0]==$value_setted)
				$checked='checked';
			else
				$checked='';
			$FunctionResult='<input type="checkbox" name="pvalue" value="'.$typeValue['value'][0].'" '.$checked.'>';
		}
	}

	if($type=='txt')
	{
		if ($output=='table')
			$FunctionResult=$value_setted;

		if($output=='form')
			$FunctionResult='<textarea cols="25" rows="5" name="pvalue">'.$value_setted.'</textarea>';
	}

	if ($type=='slct')
	{
		if ($output=='table')
			$FunctionResult=$value_setted.' ('.pEcho($typeValue['variant'][$key]).')';

		if ($output=='form')
		{
			$FunctionResult='<select name="pvalue">';
				for($i=0; $i<count($typeValue['value']); $i++)
				{
					if ($typeValue['value'][$i]==$value_setted)
						$selected='selected';
					else
						$selected='';
					$FunctionResult.='<option value="'.$typeValue['value'][$i].'" '.$selected.'>'.pEcho($typeValue['variant'][$i]).'</option>';
				}
			$FunctionResult.='</select>';
		}
	}

	if($type=='rdio')
	{
		if ($output=='table')
			$FunctionResult=$value_setted.' ('.pEcho($typeValue['variant'][$key]).')';

		if($output=='form')
		{
			for($i=0; $i<count($typeValue['value']); $i++)
			{
				if ($typeValue['value'][$i]==$value_setted)
					$checked='checked';
				else
					$checked='';

				$FunctionResult.=pEcho($typeValue['variant'][$i]).'<input type="radio" name="pvalue" value="'.$typeValue['value'][$i].'" '.$checked.'>&nbsp;';
			}

		}
	}

	return $FunctionResult;
}


function GetModuleList($moduleSelected,$incAll,$tagName,$forAllName, $target, $targetParam)
{
	global $PortalPref;

	if($target=='user')
		$queryAddTarget="WHERE modules_u_active='".$targetParam."'";
	elseif($target=='admin')
		$queryAddTarget="WHERE modules_a_active='".$targetParam."'";
	else
		$queryAddTarget="";

	$selectedModuleAll='';
	if ($moduleSelected=='all')
		$selectedModuleAll='selected';

	$selectedModuleForAll='';
	if ($moduleSelected=='portal')
		$selectedModuleForAll='selected';

	$queryLangModule=mysql_query("SELECT modules_key, modules_a_name FROM ".$PortalPref."modules ".$queryAddTarget." ORDER BY modules_key ASC");

	print'<select name="'.$tagName.'">
		<option value="-1">'._SELECT_MODULE_.'</option>';
		if ($incAll==1)
			print '<option value="all" '.$selectedModuleAll.'>'._ALL_VALUES_.'</option>';
		if ($forAllName!='')
			print'<option value="portal" '.$selectedModuleForAll.'>'.$forAllName.'</option>';
		while($resultLangModule=mysql_fetch_assoc($queryLangModule))
		{
			if ($resultLangModule['modules_key']=='index')
				$modName=_MAIN_PAGE_;
			else
				$modName=pEcho($resultLangModule['modules_a_name']);

			if ($resultLangModule['modules_key']==$moduleSelected)
				$selectedModule='selected';
			else
				$selectedModule='';
			print '<option value="'.$resultLangModule['modules_key'].'" '.$selectedModule.'>'.$modName.'</option>';
		}
	print '</select>';
}


function GetOptionTypeList($type)
{
	$functionResult='';

	$TypeArray=array();
	$TypeArray['value']=array(-1, 'txt', 'slct', 'chkbx', 'rdio');
	$TypeArray['desc']=array(_SELECTTYPE_, _TEXT_, _SELECT_, _CHECKBOX_, _RADIOBOX_);

	for($i=0; $i<count($TypeArray['value']); $i++)
	{
		if ($TypeArray['value'][$i]==$type)
			$selected='selected';
		else
			$selected='';
		$functionResult.='<option value="'.$TypeArray['value'][$i].'" '.$selected.'>'.$TypeArray['desc'][$i].'</option>';
	}

	return $functionResult;
}

function getMenuArrayAdmin($res)
{
	$hLevels = array();
	$hTree = array();
	$hCur = array();

	$menu=array();

	while($rows = mysql_fetch_assoc($res))
	{
		$hCur = &$hLevels[$rows['nav_id']];
		$hCur['nav_id'] = $rows['nav_id'];
		$hCur['nav_parent_id'] = $rows['nav_parent_id'];
		$hCur['nav_admin'] = $rows['nav_admin'];
		$hCur['nav_module'] = $rows['nav_module'];
		$hCur['nav_page'] = $rows['nav_page'];
		$hCur['nav_order'] = $rows['nav_order'];

		$hCur['nav_activity'] = $rows['nav_activity'];

		$hCur['nav_title'] = pEcho($rows['nav_title']);

		if($rows['nav_parent_id'] == 0)
			$hTree[$rows['nav_id']] = &$hCur;
        else
			$hLevels[$rows['nav_parent_id']]['children'][$rows['nav_id']] = &$hCur;
    }

	$menu=$hTree;

	return $menu;
}


function getMenuTreeAdmin($arr, $child, $shift)
{
	global $umk, $upk, $amk, $apk, $portal_url, $PortalPref;

	$out = '';

	$ChildSymbol='';

	if ($child==1)
	{
		$shift++;
		$Style='';
		$B=''; $BEnd='';
		$dd=1;
	}else
	{
		$B='<b>'; $BEnd='</b>';
	}

	if($shift>0)
	{
		for($i=0; $i<$shift; $i++)
		{
			$ChildSymbol.='&nbsp;&nbsp;';
		}
	}

	foreach($arr as $k=>$v)
	{
		$modifyIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=manage&act=3&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/modify.png" id="img-action" title="'._MODIFY_TITLE_.'"></a>';
		$delIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=1&nid='.$v['nav_id'].'" onClick="return confirm(\''._CLICKDEL_.' '._THISMENUITEM_.': '.$v['nav_title'].'\');"><img src="'.$portal_url.'/images/icon/16x16_png/delete.png" id="img-action" title="'._DEL_TITLE_.'"></a>';

		if ($v['nav_activity']==1)
			$ActivityIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=3&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/yes.png" id="img-action" title="'._ACTIVE_NAV_TITLE_Y_.'" ></a>';
		else
			$ActivityIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=2&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/warning.png" id="img-action" title="'._ACTIVE_NAV_TITLE_N_.'" ></a>';

		$queryCol=mysql_query("SELECT COUNT(nav_id) AS col, MAX(nav_order) AS max FROM ".$PortalPref."navigation WHERE nav_parent_id='".$v['nav_parent_id']."'");
		$resultCol=mysql_fetch_assoc($queryCol);
		if ($v['nav_order']==0)
		{
			if ($resultCol['col']<2)
				$arrowIcon='';
			else
				$arrowIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=4&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/down.png" id="img-action" title="'._GO_DOWN_TITLE_.'"></a>';
		}else
		{
			if($resultCol['max']==$v['nav_order'])
				$arrowIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=5&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/up.png" id="img-action" title="'._GO_UP_TITLE_.'"></a>';
			else
				$arrowIcon='<a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=4&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/down.png" id="img-action" title="'._GO_DOWN_TITLE_.'"></a><a href="'.$portal_url.'/admin.php?amk='.$amk.'&apk=change&act=5&nid='.$v['nav_id'].'"><img src="'.$portal_url.'/images/icon/16x16_png/up.png" id="img-action" title="'._GO_UP_TITLE_.'"></a>';
		}

		if ($child==1)
		{
			$dd++;$iclass = ($dd % 2) ? "even" : "odd";
			if (!empty($v['children']))
			{
				$B='<b>'; $BEnd='</b>';
				$iclass = "NavChildRoot";
			}else
			{
				$B=''; $BEnd='';
			}
		}
		else
			$iclass = "NavMainRoot";

		$out .= '<tr '.$Style.' id="'.$iclass.'"><td>'.$B.$ChildSymbol.$v['nav_title'].$BEnd.'</td><td>'.$ActivityIcon.''.$modifyIcon.''.$delIcon.''.$arrowIcon.'</td></tr>';

		if(!empty($v['children']))
			$out .= getMenuTreeAdmin($v['children'], 1, $shift);
	}

	return $out;
}

function getMenuTreeForManage($arr, $child, $shift, $selectItem)
{
	global $umk, $upk, $amk, $apk, $portal_url;

	$out = '';

	$ChildSymbol='';

	if ($child==1)
	{
		$shift++;
		$Style='';
		$B=''; $BEnd='';
		$dd=1;
	}else
	{
		$B='<b>'; $BEnd='</b>';
	}

	if($shift>0)
	{
		for($i=0; $i<$shift; $i++)
		{
			$ChildSymbol.='-';
		}
	}

	foreach($arr as $k=>$v)
	{
		if ($selectItem==$v['nav_id'])
			$selected='selected';
		else
			$selected='';
		$out .= '<option value="'.$v['nav_id'].'" '.$selected.'>'.$ChildSymbol.' '.$v['nav_title'].'</option>';

		if(!empty($v['children']))
			$out .= getMenuTreeForManage($v['children'], 1, $shift, $selectItem);
	}
	return $out;
}


function GetNavTarget($target)
{
	$functionResult='';
	$targetArray=array();
	$targetArray['value']=array(-1,0,1,2);
	$targetArray['desc']=array(_SELECT_TARGET_,_USER_TARGET_,_ADMIN_TARGET_,_EXTERNAL_TARGET_);

	for($i=0; $i<count($targetArray['value']); $i++)
	{
		if ($target==$targetArray['value'][$i])
			$select='selected';
		else
			$select='';
		$functionResult.='<option value="'.$targetArray['value'][$i].'" '.$select.'>'.$targetArray['desc'][$i].'</option>';
	}

	return $functionResult;
}

function GetNavPosition($position)
{
	$functionResult='';
	$positionArray=array();
	$positionArray['value']=array(-1,'both','h','v');
	$positionArray['desc']=array(_SELECT_PAGE_POSITION_,_IN_HV_NAVIGATION_,_IN_H_NAVIGATION_,_IN_V_NAVIGATION_);

	for($i=0; $i<count($positionArray['value']); $i++)
	{
		if ($position==$positionArray['value'][$i])
			$select='selected';
		else
			$select='';
		$functionResult.='<option value="'.$positionArray['value'][$i].'" '.$select.'>'.$positionArray['desc'][$i].'</option>';
	}

	return $functionResult;
}

function GetLangsList($avialable_langs, $avialable_langs_desc, $langSelected)
{
	for($i=0; $i<count($avialable_langs); $i++)
	{
		if ($avialable_langs[$i]==$langSelected)
			$selectedLang='selected';
		else
			$selectedLang='';
		print '<option value="'.$avialable_langs[$i].'" '.$selectedLang.'>'.$avialable_langs_desc[$i].'</option>';
	}
}

function GetLangsSelector($avialable_langs, $avialable_langs_desc)
{	global $option;

	print '<div>';
		for ($li=0; $li<count($avialable_langs); $li++)
		{
			if ($li==0)
				$classAdd='selected';
			else
				$classAdd='';
			print '<div id="lSelector_'.$avialable_langs[$li].'" class="tabLangSelector '.$classAdd.'" onclick="ChangeFormLanguage(this, \''.$option['avialable_langs'].'\');">'.$avialable_langs_desc[$li].'</div>';
		}
	print '</div>';}

function SetTitleForLang($mkey)
{	global $PortalPref, $option;
	$title=generateRandString(5, 1, $mkey);

	$query=mysql_query("SELECT lang_id FROM ".$PortalPref."langs_".$option['default_lang']." WHERE (lang_module='".$mkey."' OR lang_module='portal') AND lang_param='".$title."'");
	if (mysql_num_rows($query)==0)
		return $title;
	else
		SetTitleForLang($mkey);
}

function generateRandString($length, $type, $mkey)
{
	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	$string = '';

	if ($type==1)
	{
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$string = '_'.strtoupper($mkey).'_';
	}

	$numChars = strlen($chars);


	for ($i = 0; $i < $length; $i++)
	{
		$string .= substr($chars, rand(1, $numChars) - 1, 1);
	}

	if ($type==1)
		$string .= '_';

	return $string;
}
?>